//
//  GridViewItemMyOrder.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-20.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "GridViewItemMyOrder.h"
#import "PopoverPickerviewController.h"
#import "FileManager.h"
#import "Common.h"
#import "UIBadgeView.h"

@implementation GridViewItemMyOrder
-(id) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //设置UIView的背景图片
        imageBkg.image = [FileManager getImage:@"pluplu1_bkg.png" type:3 withRect:imageBkg.frame];
        [labelName setTextAlignment:UITextAlignmentLeft];//菜名左对齐
        [labelName setFont:[UIFont fontWithName:@"Arial" size:15.0f]];//字体大小
        
        buttonDelete = [[UIButton alloc] init];
        [buttonDelete setTag:TagOfUIButton];
        [buttonDelete setBackgroundImage:[FileManager getImage:@"btn_pludel.png" type:3 withRect:buttonDelete.frame] 
                                forState:UIControlStateNormal];
        [buttonDelete addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        labelPrice = [[UILabel alloc] init];
        [labelPrice setTag:TagOfUILabel+1];
        [labelPrice setAdjustsFontSizeToFitWidth:YES];//自动调整字体大小
        [labelPrice setFont:[UIFont fontWithName:@"Arial" size:15.0f]];//字体大小
        [labelPrice setTextColor:[UIColor whiteColor]];//字体白色
        [labelPrice setTextAlignment:UITextAlignmentRight];//价格右对齐
        [labelPrice setBackgroundColor:[UIColor clearColor]];//没有背景颜色
        
        [self addSubview:buttonDelete];
        [self addSubview:labelPrice];
        
        //设置已点数量
        UIBadgeView *badgeView=[[UIBadgeView alloc] initWithFrame:CGRectMake(-5, 0, 50, 20)];
        badgeView.badgeString = @"0";   
        badgeView.tag = TagOfUIView+5;
        badgeView.badgeColor = [UIColor redColor];
        [self addSubview:badgeView];
        [badgeView release];
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    [imageBkg setFrame:CGRectMake(0, 5, self.frame.size.width-5, self.frame.size.height-5)];
    [imageView setFrame:CGRectMake(10, 15, self.frame.size.width-25, self.frame.size.height-60)];
    [imageRecommend setFrame:CGRectMake(imageView.frame.size.width-16, imageView.frame.size.height-11, 26, 26)];
    [labelName setFrame:CGRectMake(10, self.frame.size.height-40, imageView.frame.size.width-60, 25)];
    
    [buttonDelete setFrame:CGRectMake(self.frame.size.width-26, 0, 26, 26)];
    [labelPrice setFrame:CGRectMake(labelName.frame.origin.x + labelName.frame.size.width, 
                                    labelName.frame.origin.y,
                                    imageView.frame.size.width - labelName.frame.size.width, 
                                    labelName.frame.size.height)];
    [self addSubview:buttonDelete];
    
    PluPlu *tempPlu = myOrderDish.modPluPlu;
    [imageView setImage:[FileManager getImage:tempPlu.ppImage type:0 withRect:imageView.bounds]];//设置图片
}
-(void) setData:(id)data{
    myOrderDish = (MyOrderDishes *)data;
    PluPlu *tempPlu = myOrderDish.modPluPlu;
    [imageView setImage:[FileManager getImage:tempPlu.ppImage type:0 withRect:imageView.bounds]];//设置图片
    [labelName setText:JsonField(myOrderDish.modDescripts)];//设置名称
    [labelPrice setText:PriceFormatter(myOrderDish.modPrice,JsonField(myOrderDish.modUnits))];//设置单价
    //设置推荐菜标志
    if ([tempPlu.ppRecommended isEqualToString:@"0"]) {
        imageRecommend.hidden = YES;
    }else {
        imageRecommend.hidden = NO;
    }
    
    [self setSelectedItem:YES];
}
-(void) setSelectedItem:(BOOL)selected{
    //设置已点数量    
    UIBadgeView *badgeView = (UIBadgeView *)[self viewWithTag:TagOfUIView+5];
    NSString *badgeNum = [NSString stringWithFormat:myOrderDish.modNumber];
    badgeView.badgeString = NumberFormatter(badgeNum); 
}
- (IBAction)buttonClicked:(id)sender {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    if ([AppSession.currentOrderMenu.momSendErr isEqualToString:@"1"]) {
        postN(@"SysMsg_RootView_AlertErrMsg");
        return;
    }
    
    NSInteger bid = [sender tag];
    if(bid == TagOfUIButton){//删除
        MyNSLog(@"点击删除按钮");
        UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                        message:TranslateStr(@"删除确认")
                                                       delegate:self 
                                              cancelButtonTitle:TranslateStr(@"取消") 
                                              otherButtonTitles:TranslateStr(@"确定"), nil];
        [alert show];
        [alert release];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==alertView.cancelButtonIndex) {
        MyNSLog(@"点击了取消按钮");
    }
    else{
        NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:myOrderDish.ppCode, @"ppCode", 
                               myOrderDish.modPrice,@"modPrice",
                               myOrderDish.modTime,@"modTime",
                               nil];
        postNWithInfos(@"SysMsg_MyOrderNineModeView_deleteMyOrderDish", nil, infos);
    }
}

-(void) dealloc{
    [buttonDelete release];
    [labelPrice release];
    [super dealloc];
}
@end
